<?php

require_once('../utils/session.php');
require_once('../utils/query.php');
require_once('../utils/facebookauth.php');

$drupalid = $_POST["drupalid"];
$drupalpw = $_POST["drupalpw"];

$hash = md5($drupalpw);

$user_result = execute_query("SELECT uid FROM untol1_testdru.druusers WHERE name = '%s' AND pass = '%s'", $drupalid, $hash);
if (!$user_result)
	die('{ "success":false, "message":"user query failed" }');
$row = mysql_fetch_assoc($user_result);
if (!$row)
	die('{ "success":false, "message":"bad login", "hash":"' . $hash . '" }');

$uid = $row['uid'];
$result = execute_query("INSERT INTO facebookUser (facebookID, drupaluid) VALUES ('%s', '%s')", $facebookID, $uid);

if ($result)
{
	$_SESSION['uid'] = $uid;
	$_SESSION['facebookid'] = $facebookID;
	die('{ "success":true }');
}

die('{ "success":false, "message":"failed to associate to facebook account" }');

?>
